<template>
  <a-card :bordered="false" :class="'cust-erp-sub-tab'">
    <!-- 操作按钮区域 -->
    <div class="table-operator" v-if="mainId">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel">
            <a-icon type="delete" />
            删除
          </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px">
          批量操作
          <a-icon type="down" />
        </a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :scroll="{ x: true }"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        @change="handleTableChange"
      >
        <template slot="materialCode" slot-scope="text, record">
          <div>
            <HfMaterialSearchSelectTag
              v-if="record.editable"
              :data-url="url.materialList"
              v-model="record.materialCode"
              :extra-param="{ supplierId: supplierId }"
              @change="onMaterialChange(arguments, record)"
            ></HfMaterialSearchSelectTag>
            <template v-else slot-scope="text, record">
              {{ record.materialCode }}
            </template>
          </div>
        </template>
        <template slot="number" slot-scope="text, record">
          <a-input-number v-model:value="record.number" v-if="record.selectable"></a-input-number>
          <span v-else>{{ text.toFixed(4) }}</span>
        </template>
        <template slot="totalPrice" slot-scope="text, record">
          {{ (record.number * record.taxPrice).toFixed(4) }}
        </template>
        <template slot="action" slot-scope="text, record">
          <div class="editable-row-operations">
            <span v-if="record.editable">
              <a @click="() => save(record)">保存</a>
              <a-divider type="vertical" style="background-color: grey"></a-divider>
              <a @click="() => cancel(record)">取消</a>
            </span>
            <span v-else>
              <a @click="edit(record)">编辑</a>
              <a-divider type="vertical" style="background-color: grey"></a-divider>
              <a @click="handleDelete(record.id)">删除</a>
            </span>
          </div>
        </template>
      </a-table>
    </div>

    <hfPurchaseRequestDetail-modal
      ref="modalForm"
      @ok="modalFormOk"
      :mainId="mainId"
      :supplierId="supplierId"
      :serial="sourceSerial"
    ></hfPurchaseRequestDetail-modal>
  </a-card>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import HfPurchaseRequestDetailModal from './modules/HfPurchaseRequestDetailModal'
import { deleteAction, getAction, postAction, putAction } from '@api/manage'
import { HfIODetailMixin } from '@/mixins/HfIODetailMixin'
import { HfOrderDetailMixin } from '@/mixins/HfOrderDetailMixin'
import HfMaterialSearchSelectTag from '@comp/HfManual/HfMaterialSearchSelectTag.vue'

export default {
  name: 'HfPurchaseRequestDetailList',
  mixins: [JeecgListMixin, HfOrderDetailMixin],
  components: { HfMaterialSearchSelectTag, HfPurchaseRequestDetailModal },
  props: {
    mainId: {
      type: String,
      default: '',
      required: false
    },
    supplierId: {
      type: String,
      required: false,
      default: ''
    },
    sourceSerial: {
      type: String,
      required: false,
      default: ''
    }
  },
  watch: {
    mainId: {
      immediate: true,
      handler(val) {
        if (!this.mainId) {
          this.clearList()
        } else {
          this.queryParam['sourceId'] = val
          this.loadData(1)
        }
      }
    }
  },
  data() {
    return {
      description: '采购请购单管理页面',
      disableMixinCreated: true,
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '物料代码',
          align: 'center',
          dataIndex: 'materialCode',
          scopedSlots: { customRender: 'materialCode' }
        },
        {
          title: '物料名称',
          align: 'center',
          dataIndex: 'materialName'
        },
        {
          title: '规格型号',
          align: 'center',
          dataIndex: 'sap'
        },
        {
          title: '单位',
          align: 'center',
          dataIndex: 'unit_dictText'
        },
        {
          title: '物料类型',
          align: 'center',
          dataIndex: 'materialType_dictText'
        },
        {
          title: '单箱数',
          align: 'center',
          dataIndex: 'perNumber'
        },
        {
          title: '仓库',
          align: 'center',
          dataIndex: 'warehouse_dictText'
        },
        {
          title: '数量',
          align: 'center',
          dataIndex: 'number',
          scopedSlots: { customRender: 'number' }
        },
        {
          title: '单价',
          align: 'center',
          dataIndex: 'price'
        },
        {
          title: '税率',
          align: 'center',
          dataIndex: 'tax',
          customRender: function(text) {
            return text + '%'
          }
        },
        {
          title: '含税单价',
          align: 'center',
          dataIndex: 'taxPrice'
        },
        {
          title: '价税合计',
          align: 'center',
          dataIndex: 'totalPrice',
          scopedSlots: { customRender: 'totalPrice' }
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/purchase/hfPurchaseRequest/listHfPurchaseRequestDetailByMainId',
        delete: '/purchase/hfPurchaseRequest/deleteHfPurchaseRequestDetail',
        deleteBatch: '/purchase/hfPurchaseRequest/deleteBatchHfPurchaseRequestDetail',
        exportXlsUrl: '/purchase/hfPurchaseRequest/exportHfPurchaseRequestDetail',
        importUrl: '/purchase/hfPurchaseRequest/importHfPurchaseRequestDetail',
        add: '/purchase/hfPurchaseRequest/addHfPurchaseRequestDetail',
        materialList: '/purchase/hfPurchaseprice/getUsePriceBySupplier'
      },
      dictOptions: {}
    }
  },
  created() {},
  computed: {
    importExcelUrl() {
      return `${window._CONFIG['domianURL']}/${this.url.importUrl}/${this.mainId}`
    }
  },
  methods: {
    clearList() {
      this.dataSource = []
      this.selectedRowKeys = []
      this.ipagination.current = 1
    },
    onMaterialChange([value, record], tableRow) {
      // 初始化数据
      Object.assign(tableRow, {
        materialId: record.materialId,
        materialCode: record.materialCode,
        materialName: record.materialName,
        sap: record.sap,
        unit: record.unit,
        unit_dictText: record.unit_dictText,
        materialType: record.materialType,
        materialType_dictText: record.materialType_dictText,
        perNumber: record.perNumber,
        warehouse: record.warehouse,
        warehouse_dictText: record.warehouse_dictText,
        price: record.price,
        tax: record.tax,
        taxPrice: record.taxPrice,
        saleOutTotalPrice: 0.0,
        totalPrice: 0.0
      })
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
